package com.neteast.androidclient.contact.activity;
import com.neteast.androidclient.contact.R;
import com.neteast.androidclient.contact.domain.Contact;
import com.neteast.androidclient.contact.util.LoginUtil;
import com.neteast.androidclient.contact.util.NetUtil;
import com.neteast.androidclient.contact.widget.InfoDetailPanel;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
 * 显示个人信息的页面
 * @author tiangh
 * 2012-3-12 下午5:32:41
 */
public class MeActivity extends Activity implements OnCheckedChangeListener{
	
	
	protected static final int DOWNLOAD_CONTACT = 1;
	
	private InfoDetailPanel infoDetail;
	private Contact contact;
	private Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case DOWNLOAD_CONTACT:
				progressDialog.dismiss();
				refresh();
				break;
			}
		}
	};
	private ProgressDialog progressDialog;
	private TextView tvTitle;
	private EditText etContent;
	private RadioButton btnAbout;
	private RadioButton btnHelp;

	private RelativeLayout leftPanel;

	private LinearLayout.LayoutParams params;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.me);
		
		tvTitle = (TextView) findViewById(R.id.my_state_title);
		etContent = (EditText) findViewById(R.id.my_state_content);
		
		btnAbout = (RadioButton)findViewById(R.id.btn_about);
		btnHelp = (RadioButton)findViewById(R.id.btn_help);
		btnAbout.setOnCheckedChangeListener(this);
		btnHelp.setOnCheckedChangeListener(this);
		btnHelp.setChecked(true);
		progressDialog = new ProgressDialog(this);
		init();
		showProgressDialog(progressDialog);
		new Thread(new Runnable() {
			public void run() {
				try {
					contact=NetUtil.getContactFromServer(MeActivity.this, LoginUtil.getLastLoginUserId(MeActivity.this));
					handler.sendEmptyMessage(DOWNLOAD_CONTACT);
				} catch (Exception e) {
					handler.sendEmptyMessage(DOWNLOAD_CONTACT);
					Log.e("test", e.getMessage(),e);
				}
			}
		}).start();	
	}
	private void showProgressDialog(ProgressDialog pDialog) {
		pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pDialog.setMessage("加载联系人，请稍候");
		pDialog.show();
	}
	private void init() {
		if (contact==null) {
			contact=new Contact();
			contact.ownId=LoginUtil.getLastLoginUserId(this);
			contact.contactId=LoginUtil.getLastLoginUserId(this);
		}
		infoDetail = new InfoDetailPanel(this, contact);
		params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
				ViewGroup.LayoutParams.MATCH_PARENT);
		leftPanel = (RelativeLayout) findViewById(R.id.left_panel);
		leftPanel.addView(infoDetail,params);
	}

	
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.btn_about:
				tvTitle.setText(R.string.label_my_about);
				try {
					String versionCode = getPackageManager().getPackageInfo(MeActivity.this.getPackageName(), 0).versionName;
					etContent.setText("    通讯录版本为"+versionCode);
				} catch (NameNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			case R.id.btn_help:
				tvTitle.setText(R.string.label_my_help);
				etContent.setText("    杭州华数为您服务");
				break;
			}
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode==Activity.RESULT_OK) {
			contact = (Contact) data.getSerializableExtra("contact");
			refresh();
		}
	}
	/**
	 * 刷新联系人信息
	 */
	private void refresh(){
		infoDetail=new InfoDetailPanel(this, contact);
		leftPanel.removeAllViews();
		leftPanel.addView(infoDetail, params);
	}
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putSerializable("contact", contact);
	}
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (!savedInstanceState.isEmpty()) {
			contact=(Contact) savedInstanceState.getSerializable("contact");
			refresh();
			savedInstanceState.clear();
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		leftPanel.removeAllViews();
		contact=null;
	}
}
